/*
 * @(#)UIEventServiceImp.java
 * Copyright (c) 2011 TravelSky Technology Ltd. All Right Reserved. 
 */
package com.meetingshow.privilege.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.SaveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.privilege.dao.model.EventPO;
import com.meetingshow.privilege.service.IEventService;
import com.meetingshow.privilege.service.model.EventQueryParas;
import com.meetingshow.privilege.uiservice.exception.EventNotExistException;
import com.meetingshow.privilege.uiservice.model.EventQueryParasVO;
import com.meetingshow.privilege.uiservice.model.EventVO;

public class UIEventServiceImp extends BaseUIServiceImp implements IUIEventService {

    private final static Logger logger = Logger.getLogger(UIEventServiceImp.class);
    
    private IEventService eventService;

    public IEventService getEventService() {
        return eventService;
    }

    public void setEventService(IEventService eventService) {
        this.eventService = eventService;
    }

	@Override
	public EventVO getEventById (java.lang.Integer eventId) throws BaseUIServiceException {
	    if (eventId == null) {
	        this.throwValidaterException("eventId is null.");
	    }
	    
	    EventPO eventPO = null;
	    try {
	        eventPO = getEventService().getEventById(eventId);
	    } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
	    }
	    
	    if (eventPO != null) {
	        return new EventVO(eventPO);
	    } else {
	        return null;
	    }
	}

    @Override
    public QueryResults<EventVO> queryEvent(EventQueryParasVO eventQueryParasVO) throws BaseUIServiceException {
        
        if (eventQueryParasVO == null) {
            this.throwValidaterException("eventQueryParasVO is null.");
        }
        
        validateThrowEx(eventQueryParasVO);
        
        QueryResults<EventVO> eventVOQueryResults = new QueryResults<EventVO>();
        List<EventVO> eventVOList = new ArrayList<EventVO>();
        QueryResults<EventPO> eventPOQueryResults = null;
        
        try {
            eventPOQueryResults = getEventService().queryEvent(eventQueryParasVO.toEventQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(eventPOQueryResults)) {
            for (EventPO eventPO : eventPOQueryResults.getResultList()) {
                if (eventPO != null) {
                    eventVOList.add(new EventVO(eventPO));
                }
            }
        }
        
        eventVOQueryResults.setResultList(eventVOList);
        eventVOQueryResults.setFirstResultNo(eventPOQueryResults.getFirstResultNo());
        eventVOQueryResults.setResultsPerGroup(eventPOQueryResults.getResultsPerGroup());
        eventVOQueryResults.setTotalResultsNum(eventPOQueryResults.getTotalResultsNum());
        
        return eventVOQueryResults;
    }

    @Override
    public void removeEvent(EventVO eventVO)
            throws BaseUIServiceException {
        
        if (eventVO == null) {
            this.throwValidaterException("eventVO is null");
        }
        
        validateThrowEx(eventVO, RemoveValidator.class);
        
        EventQueryParas eventQueryParas = new EventQueryParas();
        eventQueryParas.setEventId_e(String.valueOf(eventVO.getEventId()));
        QueryResults<EventPO> eventQueryResults = null;
        try {
            eventQueryResults = getEventService().queryEvent(eventQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (!QueryResults.resultsListNotNull(eventQueryResults)) {
            throw new EventNotExistException();
        }
        
        EventPO eventPO = eventQueryResults.getResultList().get(0);
        try {
            getEventService().removeEvent(eventPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
    }
    
    @Override
    public java.lang.Integer saveEvent(EventVO eventVO)
            throws BaseUIServiceException {
        if (eventVO == null) {
            this.throwValidaterException("eventVO is null");
        }
        eventVO.setCreateTime(new Date());
        
        validateThrowEx(eventVO, SaveValidator.class);
        
        EventPO eventPO = eventVO.toEventPO();
        try {
            getEventService().saveEvent(eventPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return eventPO.getEventId();
    }

    @Override
    public void updateEvent(EventVO eventVO)
            throws BaseUIServiceException {
        if (eventVO == null) {
            this.throwValidaterException("eventVO is null");
        }
        
        validateThrowEx(eventVO, UpdateValidator.class);
        
		EventQueryParas eventQueryParas = new EventQueryParas();
        eventQueryParas.setEventId_e(String.valueOf(eventVO.getEventId()));
        QueryResults<EventPO> eventQueryResults = null;
        try {
            eventQueryResults = getEventService().queryEvent(eventQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (!QueryResults.resultsListNotNull(eventQueryResults)) {
            throw new EventNotExistException();
        }
        
        EventPO oldEventPO = eventQueryResults.getResultList().get(0);
        
        oldEventPO.setEventType(eventVO.getEventType());
        oldEventPO.setFromUserId(eventVO.getFromUserId());
        oldEventPO.setFromUserName(eventVO.getFromUserName());
        oldEventPO.setToUserId(eventVO.getToUserId());
        oldEventPO.setToUserName(eventVO.getToUserName());
        oldEventPO.setToMeetingId(eventVO.getToMeetingId());
        oldEventPO.setToMeetingName(eventVO.getToMeetingName());
        oldEventPO.setMeetingId(eventVO.getMeetingId());
        oldEventPO.setMeetingName(eventVO.getMeetingName());
        oldEventPO.setMeetingEvent(eventVO.getMeetingEvent());
        oldEventPO.setCreateTime(eventVO.getCreateTime());
        
        try {
            getEventService().updateEvent(oldEventPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
    }
}